function createHTTPRequest()
{
    var xml_HTTP = false;
    /*@cc_on @*/
    /*@if (@_jscript_version >= 5)
    try
    {
        xml_HTTP = new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e)
    {
        try
        {
            xml_HTTP = new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch (e2)
        {
            xml_HTTP = false;
        }
    }
    @end @*/

    if (!xml_HTTP && typeof XMLHttpRequest != 'undefined')
    {
        xml_HTTP = new XMLHttpRequest();
    };

    return xml_HTTP;
};


function createHTTPPostRequest (url)
{
    var xml_HTTP_post = createHTTPRequest();
    xml_HTTP_post.open("POST", url, true);
	xml_HTTP_post.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');	

    return xml_HTTP_post;
};


function showAjaxLoad(document_id)
{
    document.getElementById(document_id).innerHTML = "<div class='loader'><img src='/img/loader.gif' /></div>";
};


function openMenu(menu, response)
{
    $("#" + menu).removeClass("closed").addClass("opened");
    $("#" + menu + "_content").show();
    document.getElementById(menu + "_content").innerHTML = response;
};

function closeMenu(menu)
{
    $("#" + menu + "_content").hide();
    $("#" + menu).removeClass("opened").addClass("closed");
};

var EXPAND_VIEW_BY_COMPANY = 1;
var EXPAND_VIEW_BY_COMPANY_IN_COMPANY = 2;
var EXPAND_VIEW_BY_COMPANY_IN_TYPE = 3;
var EXPAND_VIEW_BY_COMPANY_COMPANY = 4;
var EXPAND_VIEW_BY_COMPANY_SPA = 5;

var EXPAND_TYPES = 1;
var EXPAND_TYPES_IN_TYPES = 2;

var EXPAND_ACCOUNTS = 1;
var EXPAND_ACCOUNTS_IN_COMPANY = 2;
var EXPAND_ACCOUNTS_ACCOUNTS = 3;

var EXPAND_SETTINGS = 1;
var EXPAND_SETTINGS_IN_FIRMWARE = 2;
var EXPAND_SETTINGS_FIRMWARE = 3;
var EXPAND_SETTINGS_IN_DICTIONARIES = 4;



function expandMenu(params)
{
    var ajax_request = createHTTPPostRequest("/menu/" + params.controller);
    var args = "level=" + encodeURIComponent(params.level);

    switch (params.controller)
    {
        case "viewbycompany":
            switch (params.level)
            {
                case EXPAND_VIEW_BY_COMPANY:
                    showAjaxLoad("submenu_view_by_company");
                    ajaxViewByCompany({action: 'index', container: 'inner', no_apply: true});
                    break;

                case EXPAND_VIEW_BY_COMPANY_IN_COMPANY:
                    if ($("#company_" + params.company_id + "_content").is(":visible"))
                    {
                        closeMenu("company_" + params.company_id);
                        return;
                    }
                    else
                        args += "&company_id=" + encodeURIComponent(params.company_id);
                    break;

                case EXPAND_VIEW_BY_COMPANY_IN_TYPE:
                    if ($("#company_" + params.company_id + "_type_" + params.type_id + "_content").is(":visible"))
                    {
                        closeMenu("company_" + params.company_id + "_type_" + params.type_id);
                        return;
                    }
                    else
                        args += "&company_id=" + encodeURIComponent(params.company_id) + "&type_id=" + encodeURIComponent(params.type_id)
                    break;


                case EXPAND_VIEW_BY_COMPANY_SPA:
                    args += "&type_id=" + encodeURIComponent(params.type_id) + "&company_id=" + encodeURIComponent(params.company_id);
                    showAjaxLoad("submenu_view_by_company");
                    break;

                case EXPAND_VIEW_BY_COMPANY_COMPANY:
                    args += "&parent_id=" + encodeURIComponent(params.parent_id);
                    showAjaxLoad("submenu_view_by_company");
                    break;
            }
            break;


        case "types":
            switch (params.level)
            {
                case EXPAND_TYPES:
                    showAjaxLoad("submenu_types");
                    ajaxTypes({action: 'index', container: 'inner', no_apply: true});
                    break;

                case EXPAND_TYPES_IN_TYPES:
                    showAjaxLoad("submenu_types");
                    break;
            };
            break;


        case "accounts":
            switch (params.level)
            {
                case EXPAND_ACCOUNTS:
                    showAjaxLoad("submenu_accounts");
                    ajaxAccounts({action: 'index', container: 'inner', no_apply: true});
                    break;

                case EXPAND_ACCOUNTS_ACCOUNTS:
                    args += "&company_id=" + encodeURIComponent(params.company_id);
                    showAjaxLoad("submenu_accounts");
                    break;

                case EXPAND_ACCOUNTS_IN_COMPANY:
                    if ($("#accounts_company_" + params.company_id + "_content").is(":visible"))
                    {
                        closeMenu("accounts_company_" + params.company_id);
                        return;
                    }
                    else
                        args += "&company_id=" + encodeURIComponent(params.company_id);
                    break;
            }
            break;


        case "settings":
            switch (params.level)
            {
                case EXPAND_SETTINGS:
                    showAjaxLoad("submenu_settings");
                    ajaxSettings({action: 'index', container: 'inner', no_apply: true});
                    break;

                case EXPAND_SETTINGS_IN_FIRMWARE:
                    if ($("#settings_4_content").is(":visible"))
                    {
                        closeMenu("settings_4");
                        return;
                    };
                    break;

                case EXPAND_SETTINGS_FIRMWARE:
                    showAjaxLoad("submenu_settings");
                    break;

                case EXPAND_SETTINGS_IN_DICTIONARIES:
                    if ($("#settings_5_content").is(":visible"))
                    {
                        closeMenu("settings_5");
                        return;
                    };

                    break;
            };
            break;
    };


    ajax_request.onreadystatechange = function()
    {
        if (ajax_request.readyState == 4)
        {
            if (ajax_request.status == 200)
            {
                switch (params.controller)
                {
                    case "viewbycompany":
                        switch (params.level)
                        {
                            case EXPAND_VIEW_BY_COMPANY:
                                document.getElementById('submenu_view_by_company').innerHTML = ajax_request.responseText;
                                break;

                            case EXPAND_VIEW_BY_COMPANY_IN_COMPANY:
                                openMenu("company_" + params.company_id, ajax_request.responseText);
                                break;

                            case EXPAND_VIEW_BY_COMPANY_IN_TYPE:
                                openMenu("company_" + params.company_id + "_type_" + params.type_id, ajax_request.responseText);
                                break;


                            case EXPAND_VIEW_BY_COMPANY_COMPANY:
                            case EXPAND_VIEW_BY_COMPANY_SPA:
                                document.getElementById('submenu_view_by_company').innerHTML = ajax_request.responseText;
                                break;
                        };
                        break;


                    case "types":
                        switch (params.level)
                        {
                            case EXPAND_TYPES:
                            case EXPAND_TYPES_IN_TYPES:
                                document.getElementById('submenu_types').innerHTML = ajax_request.responseText;
                                break;
                        };
                        break;


                    case "accounts":
                        switch (params.level)
                        {
                            case EXPAND_ACCOUNTS:
                            case EXPAND_ACCOUNTS_ACCOUNTS:
                                document.getElementById('submenu_accounts').innerHTML = ajax_request.responseText;
                                break;

                            case EXPAND_ACCOUNTS_IN_COMPANY:
                                openMenu("accounts_company_" + params.company_id, ajax_request.responseText);
                                break;
                        }
                        break;


                    case "settings":
                        switch (params.level)
                        {
                            case EXPAND_SETTINGS:
                                document.getElementById('submenu_settings').innerHTML = ajax_request.responseText;
                                break;

                            case EXPAND_SETTINGS_IN_FIRMWARE:
                                openMenu("settings_4", ajax_request.responseText);
                                break;

                            case EXPAND_SETTINGS_FIRMWARE:
                                document.getElementById('submenu_settings').innerHTML = ajax_request.responseText;
                                break;

                            case EXPAND_SETTINGS_IN_DICTIONARIES:
                                openMenu("settings_5", ajax_request.responseText);
                                break;
                        };
                        break;
                };
            }
            else
                alert('There was a problem with the request.');
        };
    };

    ajax_request.send(args);
};


function ajaxSpa(params)
{
	var ajax_request = createHTTPPostRequest("/spa/" + params.action);
    var args = "";
	if (params.id)
		args += "&id=" + encodeURIComponent(params.id);

    if (!params.no_apply)
    {
        switch(params.action)
        {
            case "edit":
            case "add":
                ip = encodeURIComponent(document.getElementById('spa_ip').value);
                mac = encodeURIComponent(document.getElementById('spa_mac').value);
                serial = encodeURIComponent(document.getElementById('spa_serial').value);
                args += "&ip=" + ip + "&mac=" + mac + "&serial=" + serial + "&company_id=" + params.company_id + "&type_id=" + params.type_id;
                for (i = 0; i < params.ext_count; i++)
                    args += "&ext" + i + "=" + encodeURIComponent(document.getElementById('spa_ext_' + i).value);
            break;

            case "customize":
                for (var i = 0; i < params.templates_count; i++)
                    args += "&ext" + i + "_template=" + encodeURIComponent(document.getElementById("ext" + i + "_template").value);
                args += "&count=" + params.templates_count;
            break;

            case "delete":
            break;
        };

        args += "&apply=1";
    };



    showAjaxLoad(params.container);

	ajax_request.onreadystatechange = function()
	{
		if (ajax_request.readyState == 4)
	    {
            document.getElementById(params.container).innerHTML = ajax_request.responseText;

			if (ajax_request.status == 200)
			{
                if (((params.action == "add") || (params.action == "edit") || (params.action == "delete")) && (!params.no_apply) && (!params.no_refresh))
                    expandMenu({controller: "viewbycompany", level: EXPAND_VIEW_BY_COMPANY_SPA, company_id: params.company_id, type_id: params.type_id});
			};
		};
	};

	ajax_request.send(args);
};


function ajaxViewByCompany(params)
{
    var args = "";

    if (params.id)
        args += "&id=" + encodeURIComponent(params.id);

    if (!params.no_apply)
    {
        switch (params.action)
        {
            case "edit":
            case "add":
                var caption = encodeURIComponent(document.getElementById('caption').value);
                var company_type = encodeURIComponent(document.getElementById('company_type').value);
                var proxy = encodeURIComponent(document.getElementById('proxy').value);
                var vlan = encodeURIComponent(document.getElementById('vlan').value);
                var vlan_pc = encodeURIComponent(document.getElementById('vlan_pc').value);
                args += "&company_type=" + company_type + "&caption=" + caption + "&vlan=" + vlan + "&vlan_pc=" + vlan_pc + "&proxy=" + proxy;

                switch (parseInt(company_type))
                {
                    case 1: // Company
                    case 2: // Pool
                        ip = encodeURIComponent(document.getElementById('ip').value);
                        args += "&ip=" + ip;
                    break;

                    case 3: // Company in an existing pool
                        params.parent_id = document.getElementById('pool').value;
                        args += "&pool_id=" + encodeURIComponent(params.parent_id);
                    break;

                    default:
                        alert("Problem in script!");
                        return;
                };
            break;

            case "customize":
                global_template = encodeURIComponent(document.getElementById('global_template').value);
                ext_template = encodeURIComponent(document.getElementById('ext_template').value);

                args += "&global_template=" + global_template + "&ext_template=" + ext_template;
            break;
        };

        args += "&apply=1";
    };

	showAjaxLoad(params.container);
	ajax_request = createHTTPPostRequest("/viewbycompany/" + params.action);
	ajax_request.onreadystatechange = function()
        {
            if (ajax_request.readyState == 4)
            {
                document.getElementById(params.container).innerHTML = ajax_request.responseText;
                if (((params.action == "add") || (params.action == "edit") || (params.action == "delete")) && (!params.no_apply) && (!params.no_refresh))
                    expandMenu({controller: "viewbycompany", level: EXPAND_VIEW_BY_COMPANY_COMPANY, parent_id: params.parent_id});
            };
        };
	ajax_request.send(args);
};


function ajaxTypes(params)
{
    var args = "";

	if (params.id)
		args += "&id=" + encodeURIComponent(params.id);

    if (params.company_id)
    		args += "&company_id=" + encodeURIComponent(params.company_id);

    if (!params.no_apply)
    {
        switch (params.action)
        {
            case "add":
                exts = encodeURIComponent(document.getElementById("exts").value);
                args += "&exts=" + exts;
            case "edit":
                type = encodeURIComponent(document.getElementById("type").value);
                caption = encodeURIComponent(document.getElementById("caption").value);
                firmware_id = encodeURIComponent(document.getElementById("firmware_id").value);
                args += "&type=" + type + "&caption=" + caption + "&firmware_id=" + firmware_id;

            break;

            case "customize":
            case "short":
                for (i = 0; i < params.exts_count; i++)
                    args += "&ext" + i + "_template=" + encodeURIComponent(document.getElementById("ext" + i + "_template").value);
            break;
        };

        args += "&apply=1";
    };

	var ajax_request = createHTTPPostRequest("/types/" + params.action);
	showAjaxLoad(params.container);

	ajax_request.onreadystatechange = function()
        {
            if (ajax_request.readyState == 4)
            {
                document.getElementById(params.container).innerHTML = ajax_request.responseText;
                if (ajax_request.status == 200)
                {
                    if (((params.action == "add") || (params.action == "edit") || (params.action == "delete")) && (!params.no_apply) && (!params.no_refresh))
                        expandMenu({controller: "types", level: EXPAND_TYPES_IN_TYPES});
                };
            }
        };

	ajax_request.send(args);
};


function ajaxAccounts(params)
{
    var args = "";
    
    if (params.id)
        args += "&id=" + encodeURIComponent(params.id);

    if (!params.no_apply)
    {
        switch (params.action)
        {
            case "edit":
            case "add":
            	 account_login = encodeURIComponent(document.getElementById('account_login').value);
                 account_password = encodeURIComponent(document.getElementById('account_password').value);
                 account_display_name = encodeURIComponent(document.getElementById('account_display_name').value);
                 account_short_name = encodeURIComponent(document.getElementById('account_short_name').value);
                 params.company_id = document.getElementById('account_company').value;
                 args += "&login=" + account_login + "&password=" + account_password + "&display_name=" + account_display_name + "&short_name=" + account_short_name + "&company_id=" + encodeURIComponent(params.company_id);
                break;

            case "delete":
                break;
        };
        
        args += "&apply=1";
    };


    showAjaxLoad(params.container);

    ajax_request = createHTTPPostRequest("/accounts/" + params.action);
    ajax_request.onreadystatechange = function()
    {
        if (ajax_request.readyState == 4)
        {
            document.getElementById(params.container).innerHTML = ajax_request.responseText;
            if (ajax_request.status == 200)
            {
                if (((params.action == "add") || (params.action == "edit") || (params.action == "delete")) && (!params.no_apply) && (!params.no_refresh))
                    expandMenu({controller: "accounts", level: EXPAND_ACCOUNTS_ACCOUNTS, company_id: params.company_id});
            };
        };
    };
    ajax_request.send(args);
};


function ajaxSettings(params)
{
	var args = "";

    if (params.id)
        args += "id=" + encodeURIComponent(params.id);

    if (!params.no_apply)
    {

		switch(params.id)
		{
			case 1:
				global_template = encodeURIComponent(document.getElementById("global_template").value);
				global_account_template = encodeURIComponent(document.getElementById("global_account_template").value);
				args += "&global_template=" + global_template + "&global_account_template=" + global_account_template;
			break;
		};

        args += "&apply=1";
	};
	showAjaxLoad(params.container);

	ajax_request = createHTTPPostRequest("/settings/" + params.action);
	ajax_request.onreadystatechange = function()
    {
        if (ajax_request.readyState == 4)
            document.getElementById(params.container).innerHTML = ajax_request.responseText;
    };
	ajax_request.send(args);
};


function ajaxFirmwares(params)
{
	var ajax_request = createHTTPPostRequest("/firmwares/" + params.action);
    var args = "";
	if (params.id)
		args += "&id=" + encodeURIComponent(params.id);

    if (!params.no_apply)
    {
        switch(params.action)
        {
            case "index":
                args += "&upgrade_error_retry_delay=" + encodeURIComponent(document.getElementById('upgrade_error_retry_delay').value);
                break;

            case "delete":
                break;

            case "add":
            case "edit":
                args += "&caption=" + encodeURIComponent(document.getElementById('caption').value) +
                        "&version=" + encodeURIComponent(document.getElementById('version').value);
            break;
        };

        args += "&apply=1";
    };


    showAjaxLoad(params.container);

	ajax_request.onreadystatechange = function()
	{
		if (ajax_request.readyState == 4)
	    {
            document.getElementById(params.container).innerHTML = ajax_request.responseText;

			if (ajax_request.status == 200)
			{
                if (((params.action == "add") || (params.action == "edit") || (params.action == "delete")) && (!params.no_apply) && (!params.no_refresh))
                    expandMenu({controller: "settings", level: EXPAND_SETTINGS_FIRMWARE});
			};
		};
	};

	ajax_request.send(args);
};


function ajaxDictionaries(params)
{
	var ajax_request = createHTTPPostRequest("/dictionaries/" + params.action);
    var args = "";
	if (params.id)
		args += "&id=" + encodeURIComponent(params.id);

    if (!params.no_apply)
    {
        /*
        switch(params.action)
        {
            case "index":
                args += "&upgrade_error_retry_delay=" + encodeURIComponent(document.getElementById('upgrade_error_retry_delay').value);
                break;

            case "delete":
                break;

            case "add":
            case "edit":
                args += "&caption=" + encodeURIComponent(document.getElementById('caption').value) +
                        "&version=" + encodeURIComponent(document.getElementById('version').value);
            break;
        };

        args += "&apply=1";
        */
    };


    showAjaxLoad(params.container);

	ajax_request.onreadystatechange = function()
	{
		if (ajax_request.readyState == 4)
	    {
            document.getElementById(params.container).innerHTML = ajax_request.responseText;

			if (ajax_request.status == 200)
			{
                /*
                if (((params.action == "add") || (params.action == "edit") || (params.action == "delete")) && (!params.no_apply) && (!params.no_refresh))
                    expandMenu({controller: "settings", level: EXPAND_SETTINGS_FIRMWARE});
                */
			};
		};
	};

	ajax_request.send(args);
};


function addItem(params)
{
    var args = "";
    var ajax_request = createHTTPPostRequest( (params.menu == "device" ? "spa/adddevice" : (params.menu + "/add") ));

    showAjaxLoad(params.container);

    switch (params.menu)
    {
        case "device":
            args += "company_id=" + params.company_id;
            break;

        case "spa":
            args += "company_id=" + params.company_id + "&type_id=" + params.type_id;
            break;

        case "accounts":
            args += "company_id=" + params.company_id;
            break;

        case "viewbycompany":
            if (params.pool_id)
                args += "&pool_id=" + params.pool_id;
            break;
    };

    ajax_request.onreadystatechange = function()
    {
        if (ajax_request.readyState == 4)
            document.getElementById(params.container).innerHTML = ajax_request.responseText;
    };

    ajax_request.send(args);
};


function doSearch(search_text, search_table, container)
{
	text = encodeURIComponent(search_text);
	table = encodeURIComponent(search_table);
	args = "search_text="+text+"&search_table="+table;
	document.getElementById(container).innerHTML = "<div class='loader'><img src='/img/loader.gif' /></div>";
	var ajax_request = createHTTPPostRequest("/search/index");
	ajax_request.onreadystatechange = function() 
	{
		if (ajax_request.readyState == 4)
            document.getElementById(container).innerHTML = ajax_request.responseText;
	};
	ajax_request.send(args);
};


function event_onChangeCompanyType()
{
    var company_type = parseInt(document.getElementById("company_type").value);

    if (company_type == 3)
    {
        document.getElementById("ip_rd").value = "";
        document.getElementById("pool").value = "0";
    };

    document.getElementById("tr_select_pool").style.display = (company_type == 3 ? "" : "none");
    document.getElementById("tr_ip_rd").style.display = (company_type == 3 ? "" : "none");
    document.getElementById("tr_ip").style.display = (company_type == 3 ? "none" : "");
};


function event_onChangePool()
{
    var pool_id = document.getElementById("pool").value;

    var ajax_request = createHTTPPostRequest("/viewbycompany/pool");
	ajax_request.onreadystatechange = function()
	{
		if (ajax_request.readyState == 4)
		{
			if (ajax_request.status == 200)
			{
                var properties = eval('(' + ajax_request.responseText + ')');
                document.getElementById("ip_rd").value = properties["ip"];
            };
        };
    };
    ajax_request.send("id=" + pool_id);
};


function event_onChange_CompanyID(ext_id)
{
    var accounts_list = $('#spa_ext_' + ext_id);
    accounts_list.empty();
    accounts_list.append("<option value=\"-1\" selected>Empty</option>");

    if (parseInt(document.getElementById("company_id_" + ext_id).value) != -1)
    {
        var ajax_request = createHTTPPostRequest("/accounts/getlist");
        ajax_request.onreadystatechange = function()
        {
            if (ajax_request.readyState == 4)
            {
                if (ajax_request.status == 200)
                {
                    var accounts_array = eval('(' + ajax_request.responseText + ')');
                    for (var account in accounts_array)
                        accounts_list.append("<option value=\"" + accounts_array[account]["id"] + "\">Login: " + accounts_array[account]["login"] + " (Display name: " + accounts_array[account]["display_name"] + "; Short name: " + accounts_array[account]["short_name"] +  ")</option>");
                };
            };
        };
        ajax_request.send("company_id=" + document.getElementById("company_id_" + ext_id).value);
    };
};